<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:education:course:unit:point:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_education_course_unit_point_form', query: {course_id: dataForm.course_id, unit_id: dataForm.unit_id} })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:education:course:unit:point:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
          <div class="mr10">
            <el-button icon="Back" @click="router.push({ name: 'module_education_course_unit_list', query: {course_id: dataForm.course_id, unit_id: dataForm.unit_id} })">
              {{ $t('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('course.unit.point.title')" clearable>
            </el-input>
          </div>
          <div class="input">
            <el-select v-model="dataForm.is_hidden" :placeholder="$t('common.please_select') + $t('course.is_hidden')" clearable>
              <el-option :label="$t('common.all')" value=""></el-option>
              <el-option :label="$t('common.hidden')" value="1"></el-option>
              <el-option :label="$t('common.show')" value="2"></el-option>
            </el-select>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="$t('course.unit.point.info')" width="300">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.picture" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ $t('course.title') }}：
                  {{ scope.row.title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ $t('course.create_time') }}：
                  {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('course.title')" width="240">
            <template v-slot="scope">
              <span v-if="scope.row.course">{{ scope.row.course.title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="$t('course.unit.point.course_unit')">
            <template v-slot="scope">
              {{ scope.row.complete_name }}
            </template>
          </el-table-column>

          <el-table-column prop="time_length" :label="$t('course.time_length')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.statistical">
                {{ scope.row.statistical.point_time_length }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="sort" :label="i18n('common.sort')" width="80">
          </el-table-column>

          <el-table-column :label="$t('common.is_hidden')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_hidden.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #ff4949; --el-switch-off-color: #13ce66"
                @change="handleStatus($event, scope.row.id, 'is_hidden')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="380">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:education:course:unit:point:view')"
                type="info"
                icon="View"
                @click="router.push({ name: 'module_education_course_unit_point_view', query: { course_id: dataForm.course_id, unit_id: dataForm.unit_id, id: scope.row.id } })">
                {{ i18n('common.view') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:unit:point:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_education_course_unit_point_form', query: { course_id: dataForm.course_id, unit_id: dataForm.unit_id, id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:unit:point:homework:list')"
                class="el-button--yellow"
                icon="Checked"
                @click="router.push({ name: 'module_education_course_unit_point_homework_list', query: { course_id: dataForm.course_id, unit_id: dataForm.unit_id, point_id: scope.row.id } })">
                {{ i18n('course.unit.point.homework_info') }}
              </el-button>

              <el-button
                v-if="isAuth('module:education:course:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="course-unit-point-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('education/course/unit/point')

  const dataForm = reactive([
    'course_id',
    'unit_id',
    'title',
    'is_hidden',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  onMounted(() => {
    dataForm.course_id = query.course_id
    dataForm.unit_id = query.unit_id

    getDataList(model)
  })
</script>
